package com.ziJing.multithreading.sync.t_02_RefTypeAndThreadLocal;

import java.util.concurrent.TimeUnit;

/**
 * @program Multithreading
 * @description:
 * @author: zhangbo
 * @create: 2022/05/04 15:59
 */
public class T14_TestThreadLocal_01 {

    static ThreadLocal<Person> tl = new ThreadLocal<Person>();

    public static void main(String[] args) {
        new Thread(() -> {
            Person person = new Person();
            person.name = "李四";
            tl.set(person);
            System.out.println("thread1 set success!");

            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("thread1, tl:" + tl.get());
        }).start();


        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            System.out.println("thread2, tl:" + tl.get());
        }).start();

    }


    static class Person {
        String name;

        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
}
